
Ext.define('CargoForm', {
	extend : 'Ext.form.Panel',
	alias : 'widget.cargoform',
	closable: true,
	items : [{
		xtype: 'fieldcontainer',
		layout: 'hbox',
		items: [{
			xtype : 'combobox',
			name : 'customerId',
			allowBlank : false,
			fieldLabel : 'Müşteri',
			store: {
				type : 'customer'
			},
			valueField: 'id',
			queryMode: 'remote'
		}, {
			xtype: 'button',
			text: 'Yeni Müşteri',
			handler: function (btn) {
				var tabpanel = btn.up('viewport').down('tabpanel');
	    		
	    		tabpanel.add({
	    			title: 'Müşteri Formu',
	    			xtype: 'customerform'
	    		});
			}
		}]
	}, {
		xtype : 'textfield',
		name : 'targetPerson',
		allowBlank : false,
		fieldLabel : 'Alıcı Kişi'
	}, {
		xtype : 'textfield',
		name : 'targetAddress',
		allowBlank : false,
		fieldLabel : 'Alıcı Adres'
	}, {
		xtype : 'textfield',
		name : 'targetPhone',
		allowBlank : false,
		fieldLabel : 'Alıcı Telefon'
	},  {
		xtype : 'numberfield',
		name : 'weight',
		allowBlank : true,
		fieldLabel : 'Ağırlık'
	},{
		xtype : 'hidden',
		name : 'id'
	}],
	buttons : [ {
		text : 'Kaydet',
		handler : function() {
			var form = this.up('form').getForm();
			if (form.isValid()) {
				form.submit({
					url : 'AdministrationServlet?cmd=saveCargo',
					success : function(form, action) {
						Ext.Msg.alert('Success', action.result.msg);
					},
					failure : function(form, action) {
						Ext.Msg.alert('Failed', action.result.msg);
					}
				});
			}
		}
	} ],
	initComponent : function() {
		this.callParent();
	}
});


Ext.define('CargoGrid', {
	extend : 'Ext.grid.Panel',
	closable: true,
	selModel: {
	    selType: 'checkboxmodel',
	    mode: 'MULTI'
	},
	alias : 'widget.cargogrid',
	columns : [ {
		header : 'Id',
		dataIndex : 'id'
	}, {
		header : 'Müşteri',
		dataIndex : 'customerId',
		flex : 1
	}, {
		header : 'Ağırlık',
		dataIndex : 'weight',
		flex : 1
	} , {
		header : 'Alıcı Kişi',
		dataIndex : 'targetPerson',
		flex : 1
	} , {
		header : 'Alıcı Telefon',
		dataIndex : 'targetPhone',
		flex : 1
	} , {
		header : 'Durum',
		dataIndex : 'status',
		flex : 1
	} , {
		header : 'Araç',
		dataIndex : 'vechileId',
		flex : 1
	} ],
	tbar: [{
		xtype: 'button',
		text: 'Yeni',
		handler: function (btn){
			var tabpanel = btn.up('viewport').down('tabpanel');
    		
    		tabpanel.add({
    			title: 'Kargo Formu',
    			xtype: 'cargoform'
    		});
		}
	}, {
		xtype: 'button',
		text: 'Tazele',
		handler: function (btn){
			var store = btn.up('grid').getStore();
			store.load();
		}
	}, {
		xtype: 'button',
		text: 'Araca Yükle',
		tooltip: 'Seçilen kargoları araca yükle',
		handler: function (btn){
			var selection = btn.up('grid').getSelectionModel().getSelection();
			if (selection.length == 0 ) {
				alert('Satır seçiniz');
			}
			var cargoIds = [];
			for ( var i = 0; i < selection.length; i++) {
				cargoIds [i] = selection [i].get('id');
			}
			Ext.Ajax.request({
				url: 'AdministrationServlet?cmd=addCargoIntoVechile',
				params: {
					cargoIds: Ext.JSON.encode(cargoIds),
					vechileId: '111'
				},
				success: function (response) {
					var obj = Ext.JSON.decode(response.responseText);
					if (obj.success == true) {
						alert (obj.msg);
					} else {
						alert ('HATA:' + obj.msg);
					}
				}
			});
		}
	}, {
		xtype: 'button',
		text: 'Araçtan çıkar',
		tooltip: 'Seçilen kargoları aractan çıkar',
		handler: function (btn){
			var selection = btn.up('grid').getSelectionModel().getSelection();
			if (selection.length == 0 ) {
				alert('Satır seçiniz');
			}		
			
			var cargoIds = [];
			for ( var i = 0; i < selection.length; i++) {
				cargoIds [i] = selection [i].get('id');
			}
			Ext.Ajax.request({
				url: 'AdministrationServlet?cmd=removeCargoFromVechile',
				params: {
					cargoIds: Ext.JSON.encode(cargoIds),
					vechileId: '111'
				},
				success: function (response) {
					var obj = Ext.JSON.decode(response.responseText);
					if (obj.success == true) {
						alert (obj.msg);
					} else {
						alert ('HATA:' + obj.msg);
					}
				}
			});
		}
	}, {

		xtype: 'button',
		text: 'Müşteriye Teslim',
		tooltip: 'Seçilen kargoları müşteriye teslim et',
		handler: function (btn){
			var selection = btn.up('grid').getSelectionModel().getSelection();
			if (selection.length == 0 ) {
				alert('Satır seçiniz');
			}		
			
			var cargoIds = [];
			for ( var i = 0; i < selection.length; i++) {
				cargoIds [i] = selection [i].get('id');
			}
			Ext.Ajax.request({
				url: 'AdministrationServlet?cmd=deliverCargo', //kargoyu müşteriye teslim et
				params: {
					cargoIds: Ext.JSON.encode(cargoIds)
				},
				success: function (response) {
					var obj = Ext.JSON.decode(response.responseText);
					if (obj.success == true) {
						alert (obj.msg);
					} else {
						alert ('HATA:' + obj.msg);
					}
				}
			});
		}		
	}],
	store : {
		type : 'cargo'
	},
	initComponent : function() {

		this.callParent();
	}
});

Ext.define('CargoStore', {
	extend : 'Ext.data.Store',
	alias : 'store.cargo',
	fields : [ 'id', 'customerId', 'targetPerson', 'targetAddress', 'targetPhone', 'status', 'vechileId', 'weight' ],
	autoLoad : true,
	proxy : {
		type : 'ajax',
		url : 'AdministrationServlet?cmd=searchCargo',
		reader : {
			type : 'json',
			root : 'items'
		}
	}
});